#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
参数化 GTest 测试用例
"""

from gtest import (
    test,
    EXPECT_EQ,
    EXPECT_TRUE,
    EXPECT_GT,
    EXPECT_LT,
    EXPECT_NE,
    EXPECT_GE,
    EXPECT_LE,
)
from mc.types import (
    int8_t,
    int16_t,
    int32_t,
    int64_t,
    uint8_t,
    uint16_t,
    uint32_t,
    uint64_t,
    size_t,
    ptrdiff_t,
)


@test("ParameterizedMath", "PowerOfTwo_1")
def test_power_of_two_1():
    """测试 2 的幂次：2^1"""
    base: int32_t = 2
    exponent: int32_t = 1
    result: int32_t = pow(base, exponent)
    EXPECT_EQ(result, 2)


@test("ParameterizedMath", "PowerOfTwo_2")
def test_power_of_two_2():
    """测试 2 的幂次：2^2"""
    base: int32_t = 2
    exponent: int32_t = 2
    result: int32_t = pow(base, exponent)
    EXPECT_EQ(result, 4)


@test("ParameterizedMath", "PowerOfTwo_3")
def test_power_of_two_3():
    """测试 2 的幂次：2^3"""
    base: int32_t = 2
    exponent: int32_t = 3
    result: int32_t = pow(base, exponent)
    EXPECT_EQ(result, 8)


@test("ParameterizedMath", "PowerOfTwo_4")
def test_power_of_two_4():
    """测试 2 的幂次：2^4"""
    base: int32_t = 2
    exponent: int32_t = 4
    result: int32_t = pow(base, exponent)
    EXPECT_EQ(result, 16)


@test("StringValidation", "EmptyString")
def test_empty_string():
    """测试空字符串"""
    text: str = ""
    length: int32_t = len(text)
    EXPECT_EQ(length, 0)
    EXPECT_TRUE(len(text) == 0)


@test("StringValidation", "SingleChar")
def test_single_char():
    """测试单字符字符串"""
    text: str = "A"
    length: int32_t = len(text)
    EXPECT_EQ(length, 1)
    EXPECT_TRUE(len(text) > 0)


@test("StringValidation", "ShortString")
def test_short_string():
    """测试短字符串"""
    text: str = "Hello"
    length: int32_t = len(text)
    EXPECT_EQ(length, 5)
    EXPECT_TRUE(len(text) > 0)


@test("StringValidation", "LongString")
def test_long_string():
    """测试长字符串"""
    text: str = "This is a longer string for testing purposes"
    length: int32_t = len(text)
    EXPECT_EQ(length, 44)
    EXPECT_TRUE(len(text) > 0)


@test("NumberComparison", "PositiveNumbers")
def test_positive_numbers():
    """测试正数比较"""
    a: int32_t = 10
    b: int32_t = 5
    c: int32_t = 15

    EXPECT_GT(a, b)
    EXPECT_LT(a, c)
    EXPECT_GT(c, a)
    EXPECT_LT(b, a)


@test("NumberComparison", "NegativeNumbers")
def test_negative_numbers():
    """测试负数比较"""
    a: int32_t = -5
    b: int32_t = -10
    c: int32_t = -1

    EXPECT_GT(a, b)  # -5 > -10
    EXPECT_LT(a, c)  # -5 < -1
    EXPECT_GT(c, a)  # -1 > -5
    EXPECT_LT(b, a)  # -10 < -5


@test("NumberComparison", "MixedNumbers")
def test_mixed_numbers():
    """测试正负数混合比较"""
    positive: int32_t = 5
    negative: int32_t = -5
    zero: int32_t = 0

    EXPECT_GT(positive, zero)
    EXPECT_GT(zero, negative)
    EXPECT_GT(positive, negative)
    EXPECT_LT(negative, zero)
    EXPECT_LT(zero, positive)
    EXPECT_LT(negative, positive)


# ========== 不同位宽整数类型测试 ==========


@test("IntegerTypes", "Int8Type")
def test_int8_type():
    """测试 int8_t 类型"""
    small_val: int8_t = 10
    large_val: int8_t = 100
    negative: int8_t = -50

    EXPECT_EQ(small_val, 10)
    EXPECT_GT(large_val, small_val)
    EXPECT_LT(negative, small_val)
    EXPECT_NE(small_val, large_val)


@test("IntegerTypes", "Int16Type")
def test_int16_type():
    """测试 int16_t 类型"""
    val1: int16_t = 1000
    val2: int16_t = 2000
    val3: int16_t = -1000

    EXPECT_EQ(val1, 1000)
    EXPECT_LT(val1, val2)
    EXPECT_GT(val1, val3)
    EXPECT_GE(val2, val1)
    EXPECT_LE(val3, val1)


@test("IntegerTypes", "Int64Type")
def test_int64_type():
    """测试 int64_t 类型"""
    large_val: int64_t = 1000000000
    small_val: int64_t = 1000

    EXPECT_GT(large_val, small_val)
    EXPECT_EQ(large_val, 1000000000)
    result: int64_t = large_val + small_val
    EXPECT_EQ(result, 1000001000)


@test("IntegerTypes", "UnsignedInt8Type")
def test_uint8_type():
    """测试 uint8_t 类型"""
    val1: uint8_t = 0
    val2: uint8_t = 128
    val3: uint8_t = 255

    EXPECT_EQ(val1, 0)
    EXPECT_GT(val2, val1)
    EXPECT_GT(val3, val2)
    EXPECT_EQ(val3, 255)


@test("IntegerTypes", "UnsignedInt16Type")
def test_uint16_type():
    """测试 uint16_t 类型"""
    val1: uint16_t = 0
    val2: uint16_t = 32768
    val3: uint16_t = 65535

    EXPECT_EQ(val1, 0)
    EXPECT_LT(val1, val2)
    EXPECT_LT(val2, val3)


@test("IntegerTypes", "UnsignedInt32Type")
def test_uint32_type():
    """测试 uint32_t 类型"""
    val1: uint32_t = 0
    val2: uint32_t = 1000000
    val3: uint32_t = 4000000000

    EXPECT_EQ(val1, 0)
    EXPECT_GT(val2, val1)
    EXPECT_GT(val3, val2)


@test("IntegerTypes", "UnsignedInt64Type")
def test_uint64_type():
    """测试 uint64_t 类型"""
    val1: uint64_t = 0
    val2: uint64_t = 1000000000000

    EXPECT_EQ(val1, 0)
    EXPECT_GT(val2, val1)
    sum_val: uint64_t = val1 + val2
    EXPECT_EQ(sum_val, 1000000000000)


@test("IntegerTypes", "SizeType")
def test_size_t_type():
    """测试 size_t 类型"""
    size1: size_t = 0
    size2: size_t = 100
    size3: size_t = 1000

    EXPECT_EQ(size1, 0)
    EXPECT_LT(size1, size2)
    EXPECT_LT(size2, size3)
    EXPECT_GE(size3, size2)


@test("IntegerTypes", "PtrdiffType")
def test_ptrdiff_type():
    """测试 ptrdiff_t 类型"""
    diff1: ptrdiff_t = -100
    diff2: ptrdiff_t = 0
    diff3: ptrdiff_t = 100

    EXPECT_LT(diff1, diff2)
    EXPECT_GT(diff3, diff2)
    EXPECT_NE(diff1, diff3)


# ========== 整数类型与全局函数配合测试 ==========


@test("IntegerFunctions", "PowWithInt8")
def test_pow_with_int8():
    """测试 pow 函数与 int8_t"""
    base: int8_t = 2
    exponent: int8_t = 5
    result: int8_t = pow(base, exponent)
    EXPECT_EQ(result, 32)


@test("IntegerFunctions", "PowWithInt16")
def test_pow_with_int16():
    """测试 pow 函数与 int16_t"""
    base: int16_t = 3
    exponent: int16_t = 4
    result: int16_t = pow(base, exponent)
    EXPECT_EQ(result, 81)


@test("IntegerFunctions", "PowWithInt64")
def test_pow_with_int64():
    """测试 pow 函数与 int64_t"""
    base: int64_t = 2
    exponent: int64_t = 10
    result: int64_t = pow(base, exponent)
    EXPECT_EQ(result, 1024)


@test("IntegerFunctions", "PowWithUnsignedTypes")
def test_pow_with_unsigned():
    """测试 pow 函数与无符号类型"""
    base: uint32_t = 2
    exponent: uint32_t = 8
    result: uint32_t = pow(base, exponent)
    EXPECT_EQ(result, 256)


@test("IntegerFunctions", "AbsWithInt8")
def test_abs_with_int8():
    """测试 abs 函数与 int8_t"""
    negative: int8_t = -42
    result: int8_t = abs(negative)
    EXPECT_EQ(result, 42)


@test("IntegerFunctions", "AbsWithInt16")
def test_abs_with_int16():
    """测试 abs 函数与 int16_t"""
    negative: int16_t = -1234
    result: int16_t = abs(negative)
    EXPECT_EQ(result, 1234)


@test("IntegerFunctions", "AbsWithInt32")
def test_abs_with_int32():
    """测试 abs 函数与 int32_t"""
    negative: int32_t = -999999
    result: int32_t = abs(negative)
    EXPECT_EQ(result, 999999)


@test("IntegerFunctions", "AbsWithInt64")
def test_abs_with_int64():
    """测试 abs 函数与 int64_t"""
    negative: int64_t = -1000000000
    result: int64_t = abs(negative)
    EXPECT_EQ(result, 1000000000)


@test("IntegerFunctions", "MaxWithInt8")
def test_max_with_int8():
    """测试 max 函数与 int8_t"""
    a: int8_t = 10
    b: int8_t = 20
    result: int8_t = max(a, b)
    EXPECT_EQ(result, 20)


@test("IntegerFunctions", "MaxWithInt32")
def test_max_with_int32():
    """测试 max 函数与 int32_t"""
    a: int32_t = 100
    b: int32_t = 50
    c: int32_t = 75
    result: int32_t = max(a, b)
    EXPECT_EQ(result, 100)
    result2: int32_t = max(b, c)
    EXPECT_EQ(result2, 75)


@test("IntegerFunctions", "MinWithInt16")
def test_min_with_int16():
    """测试 min 函数与 int16_t"""
    a: int16_t = 1000
    b: int16_t = 2000
    result: int16_t = min(a, b)
    EXPECT_EQ(result, 1000)


@test("IntegerFunctions", "MinWithUint32")
def test_min_with_uint32():
    """测试 min 函数与 uint32_t"""
    a: uint32_t = 100
    b: uint32_t = 50
    result: uint32_t = min(a, b)
    EXPECT_EQ(result, 50)


# ========== 整数类型与 list 容器配合测试 ==========


@test("IntegerContainers", "ListInt8")
def test_list_int8():
    """测试 list 与 int8_t"""
    numbers: list[int8_t] = [10, 20, 30]

    EXPECT_EQ(len(numbers), 3)
    EXPECT_EQ(numbers[0], 10)
    EXPECT_EQ(numbers[1], 20)
    EXPECT_EQ(numbers[2], 30)


@test("IntegerContainers", "ListInt16")
def test_list_int16():
    """测试 list 与 int16_t"""
    numbers: list[int16_t] = [1000, 2000, 3000]

    EXPECT_EQ(len(numbers), 3)
    EXPECT_GT(numbers[1], numbers[0])
    EXPECT_LT(numbers[0], numbers[2])


@test("IntegerContainers", "ListInt32")
def test_list_int32():
    """测试 list 与 int32_t"""
    numbers: list[int32_t] = []
    numbers.append(10)
    numbers.append(20)
    numbers.append(30)

    EXPECT_EQ(len(numbers), 3)
    EXPECT_EQ(numbers[0], 10)
    EXPECT_EQ(numbers[2], 30)


@test("IntegerContainers", "ListInt64")
def test_list_int64():
    """测试 list 与 int64_t"""
    numbers: list[int64_t] = [1000000000, 2000000000]

    EXPECT_EQ(len(numbers), 2)
    EXPECT_EQ(numbers[0], 1000000000)
    EXPECT_EQ(numbers[1], 2000000000)


@test("IntegerContainers", "ListUint8")
def test_list_uint8():
    """测试 list 与 uint8_t"""
    numbers: list[uint8_t] = [0, 128, 255]

    EXPECT_EQ(len(numbers), 3)
    EXPECT_EQ(numbers[0], 0)
    EXPECT_EQ(numbers[2], 255)


@test("IntegerContainers", "ListUint32")
def test_list_uint32():
    """测试 list 与 uint32_t"""
    numbers: list[uint32_t] = [0, 1000000, 4000000000]

    EXPECT_EQ(len(numbers), 3)
    EXPECT_GT(numbers[2], numbers[1])


@test("IntegerContainers", "ListSizeT")
def test_list_size_t():
    """测试 list 与 size_t"""
    sizes: list[size_t] = [100, 200, 300]

    EXPECT_EQ(len(sizes), 3)
    total: size_t = sizes[0] + sizes[1] + sizes[2]
    EXPECT_EQ(total, 600)


@test("IntegerContainers", "ListOperations")
def test_list_operations():
    """测试 list 的各种操作"""
    numbers: list[int32_t] = [10, 20, 30]

    EXPECT_EQ(len(numbers), 3)

    # 修改元素
    numbers[1] = 25
    EXPECT_EQ(numbers[1], 25)

    # 访问元素
    first: int32_t = numbers[0]
    last: int32_t = numbers[2]
    EXPECT_EQ(first, 10)
    EXPECT_EQ(last, 30)


# ========== 整数类型的算术运算测试 ==========


@test("IntegerArithmetic", "AdditionInt8")
def test_addition_int8():
    """测试 int8_t 加法"""
    a: int8_t = 10
    b: int8_t = 20
    result: int8_t = a + b
    EXPECT_EQ(result, 30)


@test("IntegerArithmetic", "SubtractionInt16")
def test_subtraction_int16():
    """测试 int16_t 减法"""
    a: int16_t = 1000
    b: int16_t = 300
    result: int16_t = a - b
    EXPECT_EQ(result, 700)


@test("IntegerArithmetic", "MultiplicationInt32")
def test_multiplication_int32():
    """测试 int32_t 乘法"""
    a: int32_t = 123
    b: int32_t = 456
    result: int32_t = a * b
    EXPECT_EQ(result, 56088)


@test("IntegerArithmetic", "DivisionInt32")
def test_division_int32():
    """测试 int32_t 除法"""
    a: int32_t = 100
    b: int32_t = 5
    result: int32_t = a // b
    EXPECT_EQ(result, 20)


@test("IntegerArithmetic", "ModuloInt32")
def test_modulo_int32():
    """测试 int32_t 取模"""
    a: int32_t = 17
    b: int32_t = 5
    result: int32_t = a % b
    EXPECT_EQ(result, 2)


@test("IntegerArithmetic", "UnsignedAddition")
def test_unsigned_addition():
    """测试无符号整数加法"""
    a: uint32_t = 1000
    b: uint32_t = 2000
    result: uint32_t = a + b
    EXPECT_EQ(result, 3000)


@test("IntegerArithmetic", "UnsignedMultiplication")
def test_unsigned_multiplication():
    """测试无符号整数乘法"""
    a: uint16_t = 100
    b: uint16_t = 200
    result: uint16_t = a * b
    EXPECT_EQ(result, 20000)


@test("IntegerArithmetic", "MixedArithmetic")
def test_mixed_arithmetic():
    """测试复合算术运算"""
    a: int32_t = 10
    b: int32_t = 5
    c: int32_t = 2

    result1: int32_t = a + b * c
    EXPECT_EQ(result1, 20)

    result2: int32_t = (a + b) * c
    EXPECT_EQ(result2, 30)

    result3: int32_t = a - b + c
    EXPECT_EQ(result3, 7)
